package my.controller;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import my.bean.Admin;
import my.bean.Order;
import my.bean.Tea;
import my.bean.User;
import my.service.IAdminService;
import my.service.IOrderService;
import my.service.ITeaService;
import my.service.IUserService;
import my.service.impl.IAdminServiceImpl;
import my.service.impl.IOrderServiceImpl;
import my.service.impl.ITeaServiceImpl;
import my.service.impl.IUserServiceImpl;

@Controller
@RequestMapping("/admin")
public class AdminController {

	IAdminService adminService = new IAdminServiceImpl();
	IUserService userService = new IUserServiceImpl();
	ITeaService teaService = new ITeaServiceImpl();
	IOrderService orderService = new IOrderServiceImpl();

	@RequestMapping("/login")
	public ModelAndView login(HttpServletRequest request, String account, String password) {
		ModelAndView modelAndView = new ModelAndView();
		Admin admin = adminService.login(account, password);
		if (admin != null) {
			modelAndView = findAllTeaMess(request);
			modelAndView.addObject("admin", admin);
			request.getSession().setAttribute("admin", admin);
			// 指定视图
		} else {
			modelAndView.addObject("loginMessage", "登录失败！");
			modelAndView.setViewName("login.jsp");
		}
		return modelAndView;
	}

	@RequestMapping("/userMessage")
	public ModelAndView getUserList(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView();
		List<User> list = userService.findAll();
		if (list.size() > 0) {
			modelAndView.addObject("users", list);
			request.getSession().setAttribute("users", list);
		}
		modelAndView.setViewName("userMessage.jsp");
		return modelAndView;
	}

	@RequestMapping("/teas")
	public ModelAndView getTeaList(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView();
		List<Tea> teas = teaService.findAll();
		if (teas.size() > 0) {
			modelAndView.addObject("teas", teas);
			request.setAttribute("teas", teas);
		}
		modelAndView.setViewName("teas.jsp");
		return modelAndView;
	}

	@RequestMapping("/register")
	public ModelAndView register(HttpServletRequest request, String account, String pwd1, String pwd2, String email,
			String phone) {
		ModelAndView modelAndView = new ModelAndView();
		Admin admin = new Admin(account, pwd1, email, phone, new Date(), 0);

		if (adminService.findByAccout(account) != null) {
			modelAndView.addObject("errorMessage", "账号重复!请输入新的账号!");
			modelAndView.setViewName("register.jsp");
			return modelAndView;
		} else if (!pwd1.equals(pwd2)) {
			modelAndView.addObject("errorMessage", "密码不相同!请重新出入密码!");
			modelAndView.setViewName("register.jsp");
		} else {
			try {
				int record = adminService.register(admin);
				if (record > 0) {
					modelAndView = findAllAdmin(request);
				}

			} catch (SQLException e) {
				modelAndView.addObject("errorMessage", "发生错误!");
				modelAndView.setViewName("register.jsp");
				return modelAndView;
			}
		}
		return modelAndView;
	}

	@RequestMapping("/findAllTea")
	public ModelAndView findAllTea(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView();
		List<Tea> list = teaService.findAll();
		modelAndView.addObject("teas", list);
		modelAndView.setViewName("teas.jsp");
		return modelAndView;
	}

	@RequestMapping("/findAllTeaMess")
	public ModelAndView findAllTeaMess(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView();
		List<Tea> list = teaService.findAll();
		modelAndView.addObject("teas", list);
		modelAndView.setViewName("teasManager.jsp");
		return modelAndView;
	}

	@RequestMapping("/teaMessage")
	public ModelAndView teaMessage(HttpServletRequest request, int id) {
		ModelAndView modelAndView = new ModelAndView();
		Tea tea = teaService.findTeaById(id);
		modelAndView.addObject("tea", tea);
		modelAndView.setViewName("teaMessage.jsp");
		return modelAndView;
	}

	@RequestMapping("/teaAdder")
	public ModelAndView teaAdder(HttpServletRequest request, String name, String from, double price, double stock,
			int type, String introduction) {
		ModelAndView modelAndView = new ModelAndView();
		Tea tea = new Tea(name, from, price, stock, type, introduction);
		try {
			teaService.addTea(tea);
			modelAndView = findAllTeaMess(request);
			return modelAndView;
		} catch (SQLException e) {
			modelAndView.setViewName("teaAdder.jsp");
			return modelAndView;
		}
	}

	@RequestMapping("/teaDeleter")
	public ModelAndView teaDeleter(HttpServletRequest request, int id) {
		ModelAndView modelAndView = new ModelAndView();
		try {
			teaService.deleteTeaById(id);
			modelAndView = findAllTeaMess(request);
			return modelAndView;
		} catch (SQLException e) {
			modelAndView.setViewName("teaDeleter.jsp");
			return modelAndView;
		}
	}

	@RequestMapping("/teaModify")
	public ModelAndView teaModify(HttpServletRequest request, Integer id, String name, String from, double price,
			double stock, int type, String introduction) {
		ModelAndView modelAndView = new ModelAndView();
		Tea tea = new Tea(id, name, from, price, stock, type, introduction);
		try {
			teaService.modifyTeaMessage(tea);
			modelAndView = teaMessage(request, id);
			return modelAndView;
		} catch (SQLException e) {
			modelAndView = teaMessage(request, id);
			modelAndView.addObject("errorMessage", "修改失败!");
			return modelAndView;
		}
	}

	@RequestMapping("/pwdModify")
	public ModelAndView pwdModify(HttpServletRequest request, String oldPassword, String newPassword, String confirm) {
		ModelAndView modelAndView = new ModelAndView();
		int id = ((Admin) request.getSession().getAttribute("admin")).getId();
		String thispwd = ((Admin) request.getSession().getAttribute("admin")).getPassword();
		if (!oldPassword.equals(thispwd)) {
			modelAndView.addObject("errorMessage", "原密码不正确!");
			modelAndView.setViewName("changePwd.jsp");
		} else if (!newPassword.equals(confirm)) {
			modelAndView.addObject("errorMessage", "两次输入的密码不匹配!");
			modelAndView.setViewName("changePwd.jsp");
		} else {
			try {
				adminService.modifyAdminPasswordById(id, newPassword);
				Admin admin = adminService.findById(id);
				request.getSession().setAttribute("admin", admin);
				modelAndView = findAllTea(request);
				modelAndView.addObject("admin", admin);
			} catch (SQLException e) {
				modelAndView.addObject("errorMessage", "修改失败!");
				modelAndView.setViewName("changePwd.jsp");
				return modelAndView;
			}
		}
		return modelAndView;
	}

	@RequestMapping("/getOrderList")
	public ModelAndView getOrderList(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView();
		List<Order> list = orderService.findAll();
		modelAndView.addObject("orders", list);
		modelAndView.setViewName("orderMessage.jsp");
		return modelAndView;
	}

	@RequestMapping("/baseMessageModify")
	public ModelAndView baseMessageModify(HttpServletRequest request, String email, String phone) {
		ModelAndView modelAndView = new ModelAndView();
		Admin admin = (Admin) request.getSession().getAttribute("admin");
		admin.setEmail(email);
		admin.setPhone(phone);
		try {
			adminService.modifyBaseMessage(admin);
			modelAndView.addObject("admin", admin);
			request.getSession().setAttribute("admin", admin);
			modelAndView.setViewName("adminMessage.jsp");
			return modelAndView;
		} catch (SQLException e) {
			admin = adminService.findById(admin.getId());
			modelAndView.addObject("admin", admin);
			modelAndView.addObject("errroMessage", "修改失败!");
			modelAndView.setViewName("adminMessage.jsp");
			return modelAndView;
		}
	}

	@RequestMapping("/findAllAdmin")
	public ModelAndView findAllAdmin(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView();
		List<Admin> list = adminService.findAll();
		modelAndView.addObject("admins", list);
		modelAndView.setViewName("adminManager.jsp");
		return modelAndView;
	}

	@RequestMapping("/deleteAdmin")
	public ModelAndView deleteAdmin(HttpServletRequest request, int id) {
		ModelAndView modelAndView = new ModelAndView();
		try {
			adminService.deleteAdminById(id);
		} catch (SQLException e) {
			modelAndView.addObject("errorMessage", "删除失败!");
		}
		modelAndView = findAllAdmin(request);
		return modelAndView;
	}
	
	@RequestMapping("/conditionSearch")
	public ModelAndView conditionSearch(HttpServletRequest request,String name) {
		ModelAndView modelAndView = new ModelAndView();
		if(name == null) {
			findAllTea(request);
		}else {
			List<Tea> list = teaService.findTeaByName(name);
			modelAndView.addObject("teas", list);
			modelAndView.setViewName("teasManager.jsp");
		}
		return modelAndView;
	}
	
	@RequestMapping("/reset")
	public ModelAndView reset(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView("redirect:/admin/login.jsp");
		request.getSession().removeAttribute("admin");
		return modelAndView;
	}
}
